package com.moyq5.mvc.framework.encrypt;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.moyq5.mvc.framework.utils.ByteUtils;

public class MD5 {

	public static byte[] encrypt(byte[] data)  {
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");
			return md.digest(data);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String encrypt2Hex(byte[] data) {
		return ByteUtils.bytes2Hex(encrypt(data));
	}

	public static String encrypt2Hex(String data) {
		try {
			return ByteUtils.bytes2Hex(encrypt(data.getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String encrypt2Hex16(String data) {
		return encrypt2Hex(data).substring(8, 24);
	}

}
